const time = require('../../../util/time');
const {
    RedisPrefix
} = require('./consts')

/**
 * 检查用户任务是否存在
 * @param uid
 * @param code
 * @returns {Promise<unknown>}
 */
exports.checkDailyMission = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let dailyMissionKey = RedisPrefix.DAILY_MISSION_PREFIX + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hexists(dailyMissionKey, uid, function (err, data) {
            if (err) return reject(err);
            resolve(data != 0);
        });

    });
}

/**
 * 获取用户任务信息
 * @param uid
 */
exports.getDailyMission = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let dailyMissionKey = RedisPrefix.DAILY_MISSION_PREFIX + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hget(dailyMissionKey, uid, function (err, data) {
            if (err) return reject(err);
            resolve(JSON.parse(data));
        });

    });
}

/**
 * 获取昨日的石油总量
 * @returns {Promise<unknown>}
 */
exports.getYesterdayTotalOil = function () {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.YESTERDAY_TOTAL_OIl + time.dateFormat("YYYY_mm_dd", new Date());
        redis.exists(redisKey, function (err, data) {
            if (data != 0) {
                redis.get(redisKey, function (err, data) {
                    if (err) return reject(err);
                    resolve(parseFloat(data).toFixed(2));
                });
            } else {
                resolve(false);
            }
        });
    });
}

/**
 * 设置昨日石油总量
 * @param oil
 * @returns {Promise<unknown>}
 */
exports.setYesterdayTotalOil = function (oil) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.YESTERDAY_TOTAL_OIl + time.dateFormat("YYYY_mm_dd", new Date());
        redis.set(redisKey, oil, function (err, data) {
            if (err) return reject(err);
            resolve();
        });
    });
}

/**
 * 获取分红总量
 * @returns {Promise<unknown>}
 */
exports.getTotalProfit = function () {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.PROFIT_TOTAL + time.dateFormat("YYYY_mm_dd", new Date());
        redis.exists(redisKey, function (err, data) {
            if (data != 0) {
                redis.get(redisKey, function (err, data) {
                    if (err) return reject(err);
                    resolve(parseFloat(data).toFixed(2));
                });
            } else {
                resolve(0);
            }
        });
    });
}

/**
 * 个人分红总量
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.getPersonProfit = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.PROFIT_PERSON + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hexists(redisKey, uid, function (err, data) {
            if (err) return reject(err);
            if (data != 0) {
                redis.hget(redisKey, uid, function (err, data) {
                    if (err) return reject(err);
                    resolve(parseFloat(data).toFixed(2));
                });
            } else {
                resolve(false);
            }
        });

    });
}

/**
 * 设置昨日分红
 * @param uid
 * @param oil
 * @returns {Promise<unknown>}
 */
exports.setPersonProfit = function (uid, oil) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.PROFIT_PERSON + time.dateFormat("YYYY_mm_dd", new Date());
        redis.hset(redisKey, uid, oil, function (err, data) {
            if (err) return reject(err);
            resolve();
        });
    });
}

/**
 * 获取分红剩余
 * @returns {Promise<unknown>}
 */
exports.getSurplusProfit = function () {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let redisKey = RedisPrefix.PROFIT_SURPLUS + time.dateFormat("YYYY_mm_dd", new Date());

        redis.exists(redisKey, function (err, data) {
            if (data != 0) {
                redis.get(redisKey, function (err, data) {
                    if (err) return reject(err);
                    resolve(parseFloat(data).toFixed(2));
                });
            } else {
                resolve(0);
            }
        });

    });
}

/**
 * 领取分红奖励
 * @param oil
 */
exports.receiveProfit = function (oil) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let luaScript = require('../../../util/luaScript')(redis);
        let redisKey = RedisPrefix.PROFIT_SURPLUS + time.dateFormat("YYYY_mm_dd", new Date());
        luaScript.run('grabbingProfit', redisKey, oil
        ).then(res => {
            resolve(res);
        });
    });
}

/**
 * 初始化用户日常任务信息
 * @param mission
 */
exports.saveMission = function (mission) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let dailyMissionKey = RedisPrefix.DAILY_MISSION_PREFIX + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hset(dailyMissionKey, mission.uid, JSON.stringify(mission), function (err, data) {
            if (err) return reject(err);
            resolve(JSON.parse(data));
        });

    });
}

/**
 * 获取用户成就任务
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.getAchievementMission = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let missionKey = RedisPrefix.ACHIEVEMENT_MISSION;

        redis.hget(missionKey, uid, function (err, data) {
            if (err) return reject(err);
            resolve(JSON.parse(data));
        });

    });
}

/**
 * 更新成就任务
 * @param uid
 * @param mission
 * @returns {Promise<unknown>}
 */
exports.updAchievementMission = function (uid, mission) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let missionKey = RedisPrefix.ACHIEVEMENT_MISSION;

        redis.hset(missionKey, uid, JSON.stringify(mission), function (err, data) {
            if (err) return reject(err);
            resolve();
        });

    });
}

/**
 * 判断今日是否已经领取了分红奖励
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.isReceiveDailyBalance = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let missionKey = RedisPrefix.DAILY_BALANCE_RECEIVE + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hexists(missionKey, uid, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                resolve(false);
            } else {
                resolve(true);
            }
        });

    });
}

/**
 * 记录单日的领取记录
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.saveReceiveDailyBalance = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let missionKey = RedisPrefix.DAILY_BALANCE_RECEIVE + time.dateFormat("YYYY_mm_dd", new Date());

        redis.hset(missionKey, uid, uid, function (err, data) {
            if (err) return reject(err);
            resolve();
        });

    });
}

/**
 * 增加总视频次数
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.increaseTotalVideo = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");

        redis.hexists(RedisPrefix.PLAY_TOTAL_VIDEO, uid, function (err, data) {
            if (err) return reject(err);
            //没有记录初始化
            if (data != 1) {
                redis.hset(RedisPrefix.PLAY_TOTAL_VIDEO, uid, 1, function (err, data) {
                    resolve();
                });
            } else {
                //有记录增加
                redis.hget(RedisPrefix.PLAY_TOTAL_VIDEO, uid, function (err, data) {
                    if (err) return reject(err);
                    redis.hset(RedisPrefix.PLAY_TOTAL_VIDEO, uid, parseInt(data) + 1, function (err, data) {
                        resolve();
                    });
                });
            }
        });

    });
}

/**
 * 获取用户总视频数
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.userTotalVideo = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");

        redis.hexists(RedisPrefix.PLAY_TOTAL_VIDEO, uid, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                return resolve(0);
            } else {
                redis.hget(RedisPrefix.PLAY_TOTAL_VIDEO, uid, function (err, data) {
                    if (err) return reject(err);
                    return resolve(data);
                });
            }
        });

    });
}

/**
 * 是否完成了日常任务
 * @param key
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.checkFinishTask = function (key, uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");

        redis.hexists(key, uid, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                return resolve(false);
            } else {
                redis.hget(key, uid, function (err, data) {
                    if (err) return reject(err);
                    let obj = JSON.parse(data);
                    if (obj.personal_video_reward == 1 || obj.personal_video_reward == 2) {
                        return resolve(true);
                    } else {
                        return resolve(false);
                    }
                });
            }
        });

    });
}

/**
 * 变成有效徒弟
 * @param parent_id
 * @param uid
 */
exports.saveFinish = function (parent_id, uid) {
    let self = this;
    return new Promise((resolve, reject) => {
        let redis = self.app.get("redis");

        redis.sadd(RedisPrefix.PLAYER_VALID_SON_SET + parent_id, uid, function (err, data) {
            if (err) return reject(err);
            resolve();
        })

    });
}

/**
 * 判断是否是有效徒弟
 * @param parent_id
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.isValidSon = function (parent_id, uid) {
    let self = this;
    return new Promise((resolve, reject) => {
        let redis = self.app.get("redis");
        let key = RedisPrefix.PLAYER_VALID_SON_SET + parent_id;

        redis.exists(key, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                resolve(true);
            } else {
                redis.sismember(key, uid, function (err, data) {
                    if (err) return reject(err);
                    console.log('isValidSon data: ')
                    console.log(data)
                    if (data == 1) {
                        resolve(false);
                    } else {
                        resolve(true);
                    }
                });
            }
        });

    });
}

/**
 * 查询用户的有效徒弟数量
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.validSonNumber = function (uid) {
    return new Promise((resolve, reject) => {
        let redis = this.app.get("redis");
        let key = RedisPrefix.PLAYER_VALID_SON_SET + uid;

        redis.exists(key, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                resolve(0);
            } else {
                redis.scard(RedisPrefix.PLAYER_VALID_SON_SET + uid, function (err, data) {
                    if (err) return reject(err);
                    resolve(data);
                });
            }
        });
    });
}
